// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import VueRouter from 'vue-router'
import router from './router'
import store from './store'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import './assets/css/app.css'
import Axios from 'axios'
Vue.use(VueRouter)
Vue.use(ElementUI)
// 减少冗余的错误提示
Vue.config.productionTip = false
var loading
Axios.interceptors.request.use((config) => {
  loading = Vue.prototype.$loading({
    lock: true,
    text: '拼命加载中',
    spinner: 'el-icon-loading',
    background: 'rgba(0,0,0,0.8)'
  })
  return config
}, (err) => {
  return Promise.reject(err)
})
// 添加 响应拦截器
Axios.interceptors.response.use((res) => {
  loading && loading.close()
  return res
}, (err) => {
  return Promise.reject(err)
})

// eslint-disable-next-line no-new
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>',
  created: function () {
    //  在页面加载时读取 localStorage里的状态信息
    setTimeout(() => {
      localStorage.getItem('bookMsg') && this.$store.replaceState(Object.assign(this.$store.state, JSON.parse(localStorage.getItem('bookMsg'))))
    }, 1000)
    //  在页面刷新时将 vuex 里的信息保存到localStorge里
    window.addEventListener('beforeunload', () => {
      localStorage.setItem('bookMsg', JSON.stringify(this.$store.state))
    })
  }
})
